<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\model;

use think\Model;

class UserModel extends Model
{

    protected $type = [
        'more' => 'array',
    ];

    public function doMobile($user)
    {
        $result = $this->where('mobile', $user['mobile'])->find();
        if (!empty($result)) {
            if($user['code']!=session($user['mobile'])){
                $comparePasswordResult=0;
            }else{
                $comparePasswordResult=1;
            }
            //$comparePasswordResult = cmf_compare_password($user['user_pass'], $result['user_pass']);
            $hookParam             = [
                'user'                    => $user,
                'compare_password_result' => $comparePasswordResult
            ];
            hook_one("user_login_start", $hookParam);
            if ($comparePasswordResult) {
                //拉黑判断。
                if ($result['user_status'] == 0) {
                    return 3;
                }
                session('user', $result->toArray());
                $data = [
                    'last_login_time' => time(),
                    'last_login_ip'   => get_client_ip(0, true),
                ];
                $this->where('id', $result["id"])->update($data);
                $token = cmf_generate_user_token($result["id"], 'web');
                if (!empty($token)) {
                    session('token', $token);
                }
                return 0;
            }
            return 1;
        }
        $hookParam = [
            'user'                    => $user,
            'compare_password_result' => false
        ];
        hook_one("user_login_start", $hookParam);
        return 2;
    }
    ///TODO:: 需要调调整下。。
    public function ajaxMobileLOgin($user){
        $result = $this->where('mobile', $user['mobile'])->find();
        if(!$result){
            throw new \Exception('账户不存在');
        }
        if(isset($user['code'])){
            /* 验证码登录 */
            if($user['code']!=session($user['mobile'])){
                $comparePasswordResult=0;
            }else{
                $comparePasswordResult=1;
            }
            
            /* 验证码登录 END */
        }else if(isset($user['password'])){
            /* 密码登录 */
            if($user['password']!=session($user['mobile'])){
                $comparePasswordResult=0;
            }else{
                $comparePasswordResult=1;
            }

        }

        $hookParam             = [
                'user'                    => $user,
                'compare_password_result' => $comparePasswordResult
            ];
            hook_one("user_login_start", $hookParam);
            if ($comparePasswordResult) {
                //拉黑判断。
                if ($result['user_status'] == 0) {
                    throw new \Exception('账号被禁止访问系统');
                }
                session('user', $result->toArray());
                $data = [
                    'last_login_time' => time(),
                    'last_login_ip'   => get_client_ip(0, true),
                ];
                $this->where('id', $result["id"])->update($data);
                $token = cmf_generate_user_token($result["id"], 'web');
                if (!empty($token)) {
                    session('token', $token);
                }
                return 0;
            }

    }
    public function groups(){
        return $this->hasOne('OrganizeChildModel','id','group');
    }
//    public function communities(){
//        return $this->hasOne('CommunityModel','id','community');
//    }


}